package me.hetong.caigou.entity;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 引擎着法
 *
 * @author hetong
 */
@Data
public class EngineZhao {

    private Integer depth;// 当前搜索到的深度
    private Integer cp;// 当前得分
    private Integer nodes;// 已经搜索的节点数(即分析的局面数，它会隔一定时间显示出来；
    private Integer nps;// 引擎速度，用每秒搜索的节点数，它也会隔一定时间显示出来
    private String moves;// 原始着法列表 空格分隔
    private List<Chess> chessList;
    private StringBuilder names = new StringBuilder();
    private Integer mate;
    private List<Zhao> zhaoList = new ArrayList<>();// 转换后的着法列表

    public EngineZhao() {
    }

    public EngineZhao(String line) {
        if (StrUtil.isNotBlank(line)) {
            try {

                final String[] split = line.split(" ");

                for (int i = 0; i < split.length; i++) {
                    final String key = split[i];
                    if ("depth".equals(key)) {
                        this.setDepth(Integer.parseInt(split[i + 1]));
                    } else if ("cp".equals(key)) {
                        this.setCp(Integer.parseInt(split[i + 1]));
                    } else if ("nodes".equals(key)) {
                        this.setNodes(Integer.parseInt(split[i + 1]));
                    } else if ("mate".equals(key)) {
                        this.setMate(Integer.parseInt(split[i + 1]));
                    } else if ("nps".equals(key)) {
                        this.setNps(Integer.parseInt(split[i + 1]));
                    } else if ("pv".equals(key)) {
                        StringBuilder sb = new StringBuilder();
                        for (int j = i + 1; j < split.length; j++) {
                            final String move = split[j];
                            sb.append(move);
                            sb.append(" ");
//                            if (move.length() == 4) {
//                                Zhao zhao = FenUtils.moveToZhao(move);
//                                zhao.setRed(red);
//                                red = !red;
//                                zhao.setName(ZhaoFaUtils.getZhaoFaName(chessList, zhao));
//                                FenUtils.eatOrMove(chessList, zhao);// 修改当前chessList为最新
//                                this.getZhaoList().add(zhao);
//                                names.append(zhao.getName());
//                            }
                        }
                        this.setMoves(sb.toString());
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String toString() {
        return names.toString();
    }
}